package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//用户服务
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Override
    public Member findByTelephone(String telephone) {
        return  memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        //判断用户信息是否含有密码
        if (password!=null){
            //含有密码，需要进行加密后存储
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }
        //根据月份查询会员人数
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        //封装截止到某个月的会员人数集合
        List<Integer> list = new ArrayList<>();
        for (String month : months) { //例如： 遍历出的month格式为：2019.02
            String m = month + ".31";//月份后面拼上日期，2019.02.31
            Integer count = memberDao.findMemberCountBeforeDate(m);//查询2019。02.31之前注册的用户人数
            list.add(count);
        }
        return list;
    }
}
